<template>
	<div class="system-role-container">
		<el-card shadow="hover">
			<div class="system-user-search mb15">
        <el-form :inline="true">
          <!-- <el-form-item label="角色名称">
            <el-input size="default" v-model="tableData.param.roleName" placeholder="请输入角色名称" class="w-50 m-2" clearable/>
          </el-form-item> -->
		  <el-form-item label="筛选">
			<el-tree-select v-model="value" :data="data" filterable 
 />
          </el-form-item>
		  <el-form-item :label="value == null ?'请选择':data[value].label"> 
            <el-input
                v-model="tableData.queryParams.title"
                :placeholder="value == null ?'请输入内容':'请输入'+data[value].label" 
                clearable 
                class="w-50 m-2"
                size="default"   
				@input="keywordChange($event)"
            />
          </el-form-item>
		  <el-form-item label="日期筛选">
			<el-date-picker
        v-model="value2"
        type="daterange"
        unlink-panels
        range-separator="To"
        start-placeholder="Start date"
        end-placeholder="End date"
        :shortcuts="shortcuts"
        :size="size"
      />
          </el-form-item>
          <el-form-item>
            <el-button size="default" type="primary" class="ml10" @click="roleList">
              <el-icon>
                <ele-Search />
              </el-icon>
              查询
            </el-button>
       
			<el-button size="default" type="primary" class="ml10" @click="Refresh">
				<!-- <el-icon><Refresh /></el-icon> -->
              刷新页面
            </el-button>
			
          </el-form-item>
        </el-form>
			</div>
			<el-table :data="tableData.data" style="width: 100%">
				<!-- <el-table-column type="index" label="ID
" width="60" /> -->
			<el-table-column fixed prop="Member_id" label="ID" show-overflow-tooltip></el-table-column>
				<el-table-column prop="Member_account" label="API账户类型" width="100" ></el-table-column>
				<el-table-column prop="Name" label="用户名
" show-overflow-tooltip></el-table-column>
				<!-- <el-table-column prop="status" label="等级" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status===1">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column> -->
				<el-table-column prop="grade" label="转换类型" show-overflow-tooltip></el-table-column>
				<el-table-column prop="Whether_agent" label="转换金额" show-overflow-tooltip></el-table-column>
        <el-table-column prop="balance" label="转换前余额" show-overflow-tooltip></el-table-column>
		<el-table-column prop="Points_balance" label="转换后余额" show-overflow-tooltip></el-table-column>

		<el-table-column prop="state" label="状态" show-overflow-tooltip></el-table-column>
		<el-table-column prop="Game_balance" label="备注" show-overflow-tooltip></el-table-column>
		
		<el-table-column prop="Creation_time" label="创建时间" show-overflow-tooltip></el-table-column>
			
	
			</el-table>
      <!-- <pagination
          v-show="tableData.total>0"
          :total="tableData.total"
          v-model:page="tableData.param.pageNum"
          v-model:limit="tableData.param.pageSize"
          @pagination="roleList"
      /> -->
		</el-card>
		<!-- <EditRole ref="editRoleRef" @getRoleList="roleList"/> -->
        <MemberListadd ref="editRoleRef" @getRoleList="roleList"/>
		<Adjusting_Balance ref="Adjusting" @getRoleList="roleList"/>
	</div>
</template>

<script lang="ts">
import {toRefs, reactive, onMounted, ref, defineComponent, toRaw,getCurrentInstance} from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import MemberListadd from './component/berListadd.vue';
import Adjusting_Balance from './component/Adjusting_balance.vue';   
import {deleteRole, getRoleList} from "/@/api/MemberManagement/MemberList";

//日期筛选
const value1 = ref('')
const value2 = ref('')
const size = ref<'default' | 'large' | 'small'>('default')
const shortcuts = [
  {
    text: 'Last week',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: 'Last month',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: 'Last 3 months',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]
// 定义接口来定义对象的类型
interface TableData {
	Member_id:number;
	Member_account: number;
	grade: number;
	Name: string;
	Whether_agent: boolean;
	balance:number;
	Points_balance: number;
	Last_login_IP:number;
	Creation_time:number;
	Accumulated_recharge:number;
	state:boolean;
	Game_balance:number;
	
	// queryParams:Array<TableData>;
}

interface TableDataState {
	tableData: {
		queryParams:{
        title:string,
        component:string
      },
	  sourceData:Array<sourceDataff>;
		data: Array<TableData>;
		total: number;
		loading: boolean;
		param: {
      roleName:string;
      roleStatus:string;
			pageNum: number;
			pageSize: number;
		};
	};
}
interface sourceDataff {	
		value:string,
		label:string
}


export default defineComponent({
	name: 'apiV1SystemRoleList',
	components: {MemberListadd,Adjusting_Balance},
	setup() {


    const {proxy} = getCurrentInstance() as any;
		const addRoleRef = ref();
		const editRoleRef = ref();
		const Adjusting = ref();
    const dataScopeRef =ref();
		const state = reactive<TableDataState>({
			tableData: {
				queryParams:{
        		title:"",
        		component:""
      },
				data: [],
				sourceData:[{value:'0',label:'ID'},{value:'1',label:'用户名'},{value:'2',label:'金额范围'}],
				total: 0,
				loading: false,
				param: {
          roleName:'',
          roleStatus:'',
					pageNum: 1,
					pageSize: 10,
				},
			},
		});
	
		// 初始化表格数据
		const initTableData = () => {
			roleList()
		};
		const keywordChange = (keywords: any) =>{
			console.log(keywords)
    };
	const value = ref()
    const roleList = ()=>{
		console.log("执行搜索")
		console.log(state.tableData.queryParams.title)
		console.log(value.value)
      const data: Array<TableData> = [];
		// const data: Array<TableData> = [];
      getRoleList(state.tableData.param).then(res=>{
        const list = res.data.list??[]
        list.map((item:TableData)=>{
        //   data.push({
        //     id:item.id,
        //     status: item.status,
        //     listOrder: item.listOrder,
        //     name: item.name,
        //     remark: item.remark,
        //     dataScope:item.dataScope,
        //     createdAt: item.createdAt,
        //   });
        })
        state.tableData.data = data;
        state.tableData.total = res.data.total;
      })
    };
	
const data = ref(state.tableData.sourceData)



const filterable = (value: any) => {
	console.log("执行")
	
	console.log(state.tableData.queryParams.title)
	// console.log(value)
//   data.value = [...state.tableData.sourceData].filter((item) => item.label.includes(value))
}
		// 打开新增角色弹窗
		const onOpenAddRole = () => {
      editRoleRef.value.openDialog();
		};
		// 打开修改角色弹窗
		const onOpenEditRole = (row: Object) => {
			editRoleRef.value.openDialog(toRaw(row));
		};
// 调整余额
const Adjusting_Balanceaon = () => {
	Adjusting.value.openDialoga();
		};
		// 删除角色
		const onRowDel = (row: any) => {
			ElMessageBox.confirm(`此操作将永久删除角色：“${row.name}”，是否继续?`, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
          deleteRole(row.id).then(()=>{
            ElMessage.success('删除成功');
            proxy.$refs['editRoleRef'].resetMenuSession();
            roleList();
          })
				})
				.catch(() => {});
		};
        	// 刷新页面
		const Refresh = (row: Object) => {
			console.log("刷新页面")
		};
		
		// 分页改变
		const onHandleSizeChange = (val: number) => {
			state.tableData.param.pageSize = val;
		};
		// 分页改变
		const onHandleCurrentChange = (val: number) => {
			state.tableData.param.pageNum = val;
		};
		// 页面加载时
		onMounted(() => {
			// initTableData();
	// state.tableData.sourceData.push({value:'0',label:'ID'});
	state.tableData.data.push({
			Member_id:1,
	Member_account: 23,
	grade: 3,
	Name: '理想玩',
	Whether_agent: true,
	balance:1,
	Points_balance: 2,
	Last_login_IP:3,
	Creation_time:4,
	Accumulated_recharge:999,
	state:true,
	Game_balance:11,
          });
	
        })
      
		return {
			value,
			Adjusting,
			addRoleRef,
			editRoleRef,
     		 dataScopeRef,
			onOpenAddRole,
			onOpenEditRole,
			onRowDel,
			onHandleSizeChange,
			onHandleCurrentChange,
      roleList,
      Refresh,
	  Adjusting_Balanceaon,
			...toRefs(state),
			keywordChange,
			filterable,
			data,
			size,
	  shortcuts,
	  value2,
	  value1,
		};
	},
});
</script>
